﻿using AutoMapper;
using MediatR;
using S.Common.ApiResult;
using S.Weaning.Domain;
using S.Weaning.Domain.Dto;
using S.Weaning.Infrastructure.Interface;
using static S.Weaning.Read.ApI.Application.Command.MaternalWeaningCommand;

namespace S.Weaning.Read.ApI.Application.CommandHandlers
{
    public class MaternalWeaningCommandHandler: IRequestHandler<ShowMaternalWeaning, ApiResult<List<MaternalWeaningDto>>>,IRequestHandler<MaternalByIdCommand,MaternalWeaningModel>
    {
        private readonly IWeaningRepository maternalWeaning;
    private readonly IMapper mapper;
        private readonly IConfiguration configuration;

        public MaternalWeaningCommandHandler(IWeaningRepository maternalWeaning, IMapper mapper,IConfiguration configuration)
    {
        this.maternalWeaning = maternalWeaning;
        this.mapper = mapper;
            this.configuration = configuration;
        }
    /// <summary>
    /// 断奶显示
    /// </summary>
    /// <param name="request"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<ApiResult<List<MaternalWeaningDto>>> Handle(ShowMaternalWeaning request, CancellationToken cancellationToken)
        {
            var resultList = maternalWeaning.ShowMaternalWeaning(request.ddate, request.ear, request.build, request.userid);
            var dto = mapper.Map<List<MaternalWeaningDto>>(resultList).OrderByDescending(t => t.WeaningDate).ToList();
            ApiResult<List<MaternalWeaningDto>> result = new ApiResult<List<MaternalWeaningDto>>();
            result.Data = dto;
            result.totalCount = dto.Count;
            result.pageCount = (int)Math.Ceiling(result.totalCount * 1.0 / request.size);
            result.Data = dto.Skip((request.page - 1) * request.size).Take(request.size).ToList();
            return result;
        }
        /// <summary>
        /// 反填种母断奶信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<MaternalWeaningModel> Handle(MaternalByIdCommand request, CancellationToken cancellationToken)
        {
           var model= maternalWeaning.GetAll().FirstOrDefault(t => t.MaternalWeaningId == request.id);
            return model;
        }

    }
}
